<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <div th:include="common/head_info::head_infor"></div>
    <title>新增路线</title>
</head>
<body>
<div class="container center">
    <h2>新增路线</h2>
    <form role="form" id="myForm">
        <div class="form-group">
            <label for="routeName">路线名称</label>
            <input type="text" class="form-control" id="routeName"  placeholder="请输入名称">
        </div>
        <div class="form-group">
            <label for="routeType">路线类型</label>
            <select class="form-control "id="routeType">
                <option value="0">省内路线</option>
                <option value="1">省外路线</option>
                <option value="2">国外路线</option>
            </select>
        </div>
        <div class="form-group">
            <label for="title">路线标题</label>
            <input type="text" class="form-control" id="title" placeholder="请输入标题">
        </div>
        <div class="form-group">
            <label for="detail">路线介绍</label>
            <textarea class="form-control" rows="3" id="detail"></textarea>
        </div>
        <div class="form-group">
            <label for="file">产品主图</label>
            <input type="file" id="file">
        </div>
        <div class="form-group">
            <label for="cost">花费详情</label>
            <textarea class="form-control" rows="3" id="cost"></textarea>
        </div>
        <div class="form-group">
            <label for="prompt">提示</label>
                <textarea class="form-control" rows="3" id="prompt"></textarea>
        </div>
        <button type="button" id="submit" class="btn btn-default">提交</button>
    </form>
</div>
<script>
    //没带文件
    // function getFormData() {
    //     var routeName = $('#routeName').val();
    //     var routeType = $('#routeType').val();
    //     var title = $('#title').val();
    //     var detail = $('#detail').val();
    //     var cost = $('#cost').val();
    //     var prompt = $('#prompt').val();
    //     var parm = {routeName:routeName,routeType:routeType,title:title,detail:detail,cost:cost,prompt:prompt};
    //     return parm;
    // }
    //带文件
    function getFormData() {
        var formData = new FormData();
        formData.append("routeName",$('#routeName').val());
        formData.append("routeType",$('#routeType').val());
        formData.append("title",$('#title').val());
        formData.append("detail",$('#detail').val());
        formData.append("cost",$('#cost').val());
        formData.append("prompt",$('#prompt').val());
        formData.append("file",$('#file')[0].files[0]);
        return formData;
    }
    $('#submit').click(function () {
        var formData = getFormData();
        $.ajax({
            url:"/route/add",
            type:"post",
            dataType:"json",
            data:formData,
            processData:false,
            contentType:false,
            success:function (json) {
               $("#myForm")[0].reset();
                alert("新增成功");
            },error:function () {
               alert("新增失败")
            }
        })
    })
</script>
</body>
</html>